﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class campaignGeoOpensForCountryOutput : Api_BaseOutput
   {
      public List<campaignGeoOpensForCountryResults> result { get; set; }
      public campaignGeoOpensForCountryParms inputParms { get; private set; }
      // -------------------------------------------------------------------
      public campaignGeoOpensForCountryOutput( campaignGeoOpensForCountryInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<campaignGeoOpensForCountryResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Geo Opens: {0}", this.result.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_campaignGeoOpensForCountry[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int it;
            List<campaignGeoOpensForCountryResults> results = new List<campaignGeoOpensForCountryResults>();

            IEnumerable<XElement> countries = xml.Elements( "struct" );
            foreach ( XElement country in countries )
            {
               campaignGeoOpensForCountryResults geo = new campaignGeoOpensForCountryResults();
               geo.code = country.Element( "code" ).Value;
               geo.name = country.Element( "name" ).Value ?? string.Empty;
               int.TryParse( country.Element( "opens" ).Value, out it );
               geo.opens = it;
               results.Add( geo );
            }
            this.result = results;
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_campaignGeoOpensForCountry[] JSONResult = jss.Deserialize<x_campaignGeoOpensForCountry[]>( this.api_Response );
               this.result = FormatResults( JSONResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- 
      private List<campaignGeoOpensForCountryResults> FormatResults( x_campaignGeoOpensForCountry[] results_array )
      {
         List<campaignGeoOpensForCountryResults> results = new List<campaignGeoOpensForCountryResults>();
         //
         foreach ( x_campaignGeoOpensForCountry item in results_array )
         {
            campaignGeoOpensForCountryResults geo = new campaignGeoOpensForCountryResults();
            geo.code = item.code ?? string.Empty;
            geo.name = item.name ?? string.Empty;
            geo.opens = item.opens;
            results.Add( geo );
         }
         //
         return results;
      }

      #endregion
   }
}
